import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req, UseGuards } from '@nestjs/common';
import { LogService } from './log.service';
import { CreateLogDto } from './dto/create-log.dto';
import { UpdateLogDto } from './dto/update-log.dto';
import { PaginationLogDto } from './dto/pagination-log.dto';
import { Request } from 'express';
import { AuthGuard } from 'src/auth/auth.guard';
import { User } from 'src/user/entities/user.entity';

@Controller('/mg/log')
export class LogController {
    constructor(private readonly logService: LogService) { }

    @UseGuards(AuthGuard)
    @Post()
    create(@Body() createLogDto: CreateLogDto, @Req() request: Request) {
        console.log(request)
        const user = request.user as any;
        let ip = (request as any).ip;
        return this.logService.create(createLogDto, { ip, userId: user._id });
    }

    @UseGuards(AuthGuard)
    @Get()
    findAll(@Query() paginationLogDto: PaginationLogDto, @Req() request: Request) {
        const user: User = request.user as any;
        console.log("##user", user)
        console.log("##username", user.username);

        let ip = (request as any).ip
        console.log("#### ip", ip)
        return this.logService.findAll(paginationLogDto);
    }

    // @UseGuards(AuthGuard)
    @Post("all")
    getAll(@Body() paginationLogDto: PaginationLogDto, @Req() request: Request) {
        const user: User = request.user as any;
        console.log("##user", user)
        return this.logService.getAll(paginationLogDto);
    }


    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.logService.findOne(id);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() updateLogDto: UpdateLogDto) {
        return this.logService.update(id, updateLogDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.logService.remove(id);
    }
}
